package com.spring.authManage.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.spring.authManage.dao.UserInfoDao;
import com.spring.authManage.pojo.UserInfo;
import com.spring.authManage.pojo.UserRoleInfo;
import com.spring.common.dao.MySqlBaseDao;
import com.spring.common.util.ChunQiuTool;

/**
 * 用户管理，用户权限分配dao层实现
 *
 */
@Repository("userInfoDao")
public class UserInfoDaoImpl extends MySqlBaseDao implements UserInfoDao{

	/**
	 * 根据用户登录账号查询用户信息
	 * @param username
	 * @return
	 */
	public UserInfo queryUserInfoByUserName(String userName) throws Exception {
		UserInfo userInfo = new UserInfo();
		ArrayList<String> roleNameList = new ArrayList<String>();
		List<UserInfo> userList = this.getSqlMapClientTemplate().queryForList("queryUserInfoByUserName", userName);
		if (userList != null && userList.size() > 0) {
			for (UserInfo dto : userList) {
				userInfo.setUserName(dto.getUserName());
				userInfo.setPassword(dto.getPassword());
				userInfo.setStatus(dto.getStatus());
				userInfo.setId(dto.getId());
				if (!"".equals(ChunQiuTool.convertNull(dto.getRoleName())))
					roleNameList.add(dto.getRoleName());
			}
			userInfo.setRoleNameList(roleNameList);
		}
		return userInfo;
	}
	/**
	 * 添加用户
	 * @param userInfo
	 */
	public void addUserInfo(UserInfo userInfo) throws Exception {
		this.getSqlMapClientTemplate().insert("addUser", userInfo);
	}
	/**
	 * 修改后台登陆用户密码
	 * 
	 * @param userInfo
	 * @return int
	 */
	public int updatePwd(Map<String,Object> map) throws Exception{
		return this.getSqlMapClientTemplate().update("updatePwd",map);
	}
	
	@Override
	public void resetPwd(Map<String,Object> map) throws Exception{
		this.getSqlMapClientTemplate().update("resetPwd",map);
	}
	/**
	 * 根据用户ID获取用户信息
	 * @param userId
	 * @return
	 */
	public UserInfo queryUserInfoByUserId(String userId) throws Exception {
		return (UserInfo) this.getSqlMapClientTemplate().queryForObject("queryUserInfoByUserId", userId);
	}
	/**
	 * 更新用户信息
	 * @param userInfo
	 * @return
	 */
	public void updateUserInfo(UserInfo userInfo) throws Exception {
		 this.getSqlMapClientTemplate().update("updateUserInfo", userInfo);
	}
	/**
	 * 分配用户角色
	 * @param infos
	 */
	public void addUserRoles(List<UserRoleInfo> infos) throws Exception {
		this.getSqlMapClientTemplate().insert("addUserRole", infos);
	}
	/**
	 * 删除用户所有角色
	 * @param userId
	 */
	public void deleteUserRoles(String userId) throws Exception {
		this.getSqlMapClientTemplate().delete("deleteUserRole", userId);
	}

	/**
	 * 根据条件查询用户
	 * @param param
	 * @return
	 */
	public UserInfo queryUserInfoByParam(UserInfo userInfo) throws Exception {
		int totalRecord=0;
		if(userInfo.pageResult.getIsPaging()){
			Object obj=this.getSqlMapClientTemplate().queryForObject("countUserByParam",userInfo);
			totalRecord=Integer.parseInt(obj.toString());
			userInfo.pageResult.setTotalRecord(totalRecord);
		}
		if(totalRecord>0 || !userInfo.pageResult.getIsPaging()){			
			userInfo.pageResult.setList(this.getSqlMapClientTemplate().queryForList("queryUserByParam",userInfo));
		}
		return userInfo;
	}
	/**
	 * 根据USERNAME查询用户存在个数
	 * @param userName
	 * @return
	 */
	public Integer countExistUserByUserName(UserInfo userInfo) throws Exception {
		Integer cnt = (Integer) this.getSqlMapClientTemplate().queryForObject("countExistUserByUserName", userInfo);
		return cnt;
	}
	
	/**
	 * 根据user_id获取用户角色
	 * @param user_id
	 * @return
	 */
	public List<UserRoleInfo> queryUserRoleInfoByUserId(String user_id) throws Exception {
		return this.getSqlMapClientTemplate().queryForList("queryUserRoleInfoByUserId",user_id);
	}
	/**
	 * 删除用户
	 * @param userInfo
	 * @return
	 */
	public void deleteUser(String id) throws Exception {
		this.getSqlMapClientTemplate().update("deleteUser",id);
	}
	/**
	 * 重启用户
	 * @param userInfo
	 * @return
	 */
	public void restartUser(String id) throws Exception {
		this.getSqlMapClientTemplate().update("restartUser",id);
	}
}
